import 'dart:async';

import 'package:connectivity/connectivity.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter_device_id/flutter_device_id.dart';
import 'package:flutter_device_identifier/flutter_device_identifier.dart';
import 'package:get/get.dart';
import 'package:get/get_connect/http/src/request/request.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../utils/sp_util.dart';

FutureOr<Request> requestInterceptor(request) async {

  // StringBuffer sb = StringBuffer(request.url);
  // sb.write('?os=${GetPlatform.isAndroid ? 2 : GetPlatform.isIOS ? 1 : 0}');
  //
  // var connectivityResult = await (Connectivity().checkConnectivity());
  // if (connectivityResult == ConnectivityResult.mobile) {
  //   sb.write('&network=mobile');
  // } else if (connectivityResult == ConnectivityResult.wifi) {
  //   sb.write('&network=wifi');
  // }
  //
  // final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  // if(GetPlatform.isAndroid){
  //   sb.write('&model=${(await deviceInfoPlugin.androidInfo).model}');
  // } else if(GetPlatform.isIOS){
  //   // sb.write('&model=${(await deviceInfoPlugin.iosInfo).model}');
  // }
  //
  // PackageInfo packageInfo = await PackageInfo.fromPlatform();
  // sb.write('&version=${packageInfo.version}');
  //
  //
  //
  // if(GetPlatform.isAndroid){
  //   // sdk的版本
  //   int sdkInt = (await deviceInfoPlugin.androidInfo).version.sdkInt;
  //   if(sdkInt < 29){ // 获取imei
  //     String imei = await FlutterDeviceIdentifier.imeiCode;
  //   }else{ // 获取oaid
  //   }
  // }
  //
  // String? deviceId = await FlutterDeviceId().getDeviceId() ?? '';
  // print('object');
  // print(deviceId);
  //
  //
  // String imei = SpUtil.read(SpUtil.IMEI,'');
  //  if(imei.isNotEmpty){
  //    sb.write('&device_id=$imei');
  //  }else{
  //    String oaid = SpUtil.read(SpUtil.OAID,'');
  //    sb.write('&device_id=$oaid');
  //  }
  //
  // sb.write('&device=${GetPlatform.isAndroid ? 'Android' : GetPlatform.isIOS ? 'iOS' : '其他'}');





  var token = SpUtil.read(SpUtil.TOKEN, '');
  if('$token'.isNotEmpty){
    request.headers['token'] = '$token';
  }

  print('token = $token');
  print(request.url);

  return request;


}
